package cn.iocoder.yudao.module.handguard.service.product;

import cn.iocoder.yudao.module.handguard.controller.admin.publicitytype.vo.PsReqsVO;
import cn.iocoder.yudao.module.handguard.controller.admin.publicitytype.vo.PsResVO;
import cn.iocoder.yudao.module.handguard.controller.admin.publicitytype.vo.PublicitySelectReqsVO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.matchinginfo.MatchingInfoDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.publicitytype.PublicityTypeDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.matchinginfo.MatchingInfoMapper;
import cn.iocoder.yudao.module.handguard.dal.mysql.publicitytype.PublicityTypeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.handguard.controller.admin.product.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.product.ProductDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.handguard.convert.product.ProductConvert;
import cn.iocoder.yudao.module.handguard.dal.mysql.product.ProductMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.handguard.enums.ErrorCodeConstants.*;

/**
 * 产品管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private PublicityTypeMapper publicityTypeMapper;

    @Resource
    private MatchingInfoMapper matchingInfoMapper;

    @Override
    public Long createProduct(ProductCreateReqVO createReqVO) {
        //倍数
        if (createReqVO.getProductType() == 8 || createReqVO.getProductType() == 9 ||  createReqVO.getProductType() == 10 ){
            if (createReqVO.getMultiple()==null){
                throw exception(PRODUCT_MULTIPLE_NOT_EXISTS);
            }
        }

        //X倍、Y倍
        if ( createReqVO.getProductType()==2 ||  createReqVO.getProductType()==4 ||  createReqVO.getProductType()==11
                ||  createReqVO.getProductType()==12  ||  createReqVO.getProductType()==13){
            if (createReqVO.getMultiple()==null){
                throw exception(PRODUCT_MULTIPLE_D_NOT_EXISTS);
            }
            if (createReqVO.getMultipleTwo()==null){
                throw exception(PRODUCT_MULTIPLE_G_NOT_EXISTS);
            }
        }

        //成数X、Y
        if (createReqVO.getProductType() == 1 || createReqVO.getProductType() == 5  || createReqVO.getProductType() == 7
                || createReqVO.getProductType() == 8 || createReqVO.getProductType() == 9 ||  createReqVO.getProductType() == 10
                || createReqVO.getProductType() == 14 || createReqVO.getProductType()==15 || createReqVO.getProductType() == 16){
            if (createReqVO.getPortions()==null){
                throw exception(PRODUCT_PORTIONS_NOT_EXISTS);
            }
            if (createReqVO.getPortionsTwo()==null){
                throw exception(PRODUCT_PORTIONS_TWO_NOT_EXISTS);
            }
        }

        //成数X1、Y1
        if (createReqVO.getProductType()==10 || createReqVO.getProductType()==14){
            if (createReqVO.getAlternativePortionsOne()==null){
                throw exception(PRODUCT_PORTIONS_X1_NOT_EXISTS);
            }
            if (createReqVO.getAlternativePortionsTwo()==null){
                throw exception(PRODUCT_PORTIONS_Y1_NOT_EXISTS);
            }
        }
        // 插入
        ProductDO product = ProductConvert.INSTANCE.convert(createReqVO);
        productMapper.insert(product);
        //插入基本核查信息数据
        saveBasic(product.getId());
        // 返回
        return product.getId();
    }

    @Override
    public void updateProduct(ProductUpdateReqVO updateReqVO) {
        // 校验存在
        validateProductExists(updateReqVO.getId());

        //倍数
        if (updateReqVO.getProductType() == 8 || updateReqVO.getProductType() == 9 ||  updateReqVO.getProductType() == 10 ){
            if (updateReqVO.getMultiple()==null){
                throw exception(PRODUCT_MULTIPLE_NOT_EXISTS);
            }
        }

        //X倍、Y倍
        if ( updateReqVO.getProductType()==2 ||  updateReqVO.getProductType()==4  ||  updateReqVO.getProductType()==11
                ||  updateReqVO.getProductType()==12 ||  updateReqVO.getProductType()==13 ){
            if (updateReqVO.getMultiple()==null){
                throw exception(PRODUCT_MULTIPLE_D_NOT_EXISTS);
            }
            if (updateReqVO.getMultipleTwo()==null){
                throw exception(PRODUCT_MULTIPLE_G_NOT_EXISTS);
            }
        }

        //成数X、Y
        if (updateReqVO.getProductType() == 1 ||   updateReqVO.getProductType() == 5  || updateReqVO.getProductType() == 7
                || updateReqVO.getProductType() == 8 || updateReqVO.getProductType() == 9 ||  updateReqVO.getProductType() == 10
                || updateReqVO.getProductType() == 14 || updateReqVO.getProductType()==15 || updateReqVO.getProductType() == 16){
            if (updateReqVO.getPortions()==null){
                throw exception(PRODUCT_PORTIONS_NOT_EXISTS);
            }
            if (updateReqVO.getPortionsTwo()==null){
                throw exception(PRODUCT_PORTIONS_TWO_NOT_EXISTS);
            }
        }

        //成数X1、Y1
        if (updateReqVO.getProductType()==10 || updateReqVO.getProductType()==14){
            if (updateReqVO.getAlternativePortionsOne()==null){
                throw exception(PRODUCT_PORTIONS_X1_NOT_EXISTS);
            }
            if (updateReqVO.getAlternativePortionsTwo()==null){
                throw exception(PRODUCT_PORTIONS_Y1_NOT_EXISTS);
            }
        }
//        if ( updateReqVO.getProductType()==8 || updateReqVO.getProductType()==9 || updateReqVO.getProductType() == 10){
//            if (updateReqVO.getPortions()==null){
//                throw exception(PRODUCT_PORTIONS_ONE_NOT_EXISTS);
//            }
//            if (updateReqVO.getMultiple()==null){
//                throw exception(PRODUCT_MULTIPLE_NOT_EXISTS);
//            }
//        }
        // 更新
        ProductDO updateObj = ProductConvert.INSTANCE.convert(updateReqVO);
        productMapper.updateById(updateObj);
    }

    @Override
    public void deleteProduct(Long id) {
        // 校验存在
        validateProductExists(id);
        // 删除
        productMapper.deleteById(id);
    }

    private void validateProductExists(Long id) {
        if (productMapper.selectById(id) == null) {
            throw exception(PRODUCT_NOT_EXISTS);
        }
    }

    @Override
    public ProductDO getProduct(Long id) {
        return productMapper.selectProductById(id);
    }

    @Override
    public List<ProductDO> getProductList() {
        LambdaQueryWrapper<ProductDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductDO::getStatus,0);
        wrapper.orderByDesc(ProductDO::getCreateTime);
        List<ProductDO> productDOList = productMapper.selectList(wrapper);
        return productDOList;
    }

    @Override
    public PageResult<ProductDO> getProductPage(ProductPageReqVO pageReqVO) {
        PageResult<ProductDO> pageResult = productMapper.selectPage(pageReqVO);
        if (pageResult.getList().size()>0){
            List<ProductDO> productDOList = pageResult.getList();
            for (ProductDO productDO :productDOList){
                PublicityTypeDO publicityTypeDO = publicityTypeMapper.selectById(productDO.getProductType());
                productDO.setProductTypeName(publicityTypeDO.getTypeNames());
            }
            pageResult.setList(productDOList);
        }
        return pageResult;
    }

    @Override
    public List<ProductDO> getProductList(ProductExportReqVO exportReqVO) {
        return productMapper.selectList(exportReqVO);
    }

    @Override
    @Transactional
    public Long createOrUpdateProducts(PsReqsVO reqsVO) {
        //saveBasic(reqsVO.getId());
        if (reqsVO.getInputList().size()>0){
            for (PublicitySelectReqsVO vo :reqsVO.getInputList()){
                MatchingInfoDO matchingInfoDO = new MatchingInfoDO();
                BeanUtils.copyProperties(vo,matchingInfoDO);
                matchingInfoDO.setProductId(String.valueOf(reqsVO.getId()));
                if (vo.getId() !=null){
                    matchingInfoMapper.updateById(matchingInfoDO);
                }else {
                    matchingInfoMapper.insert(matchingInfoDO);
                }
            }
        }
        return 1l;
    }

    @Override
    public List<PsResVO> getProductsList(Long id) {
        List<PsResVO> voList = new ArrayList<>();
        LambdaQueryWrapper<MatchingInfoDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MatchingInfoDO::getProductId,id);
        List<MatchingInfoDO> doList = matchingInfoMapper.selectList(queryWrapper);
        for (MatchingInfoDO m : doList){
            PsResVO resVO = new PsResVO();
            BeanUtils.copyProperties(m,resVO);
            voList.add(resVO);
        }
        return voList;
    }

    //添加基本固定
    private void saveBasic(Long productId){
        List<MatchingInfoDO> list = new ArrayList<>();
        MatchingInfoDO m1 = new MatchingInfoDO();
        m1.setProductId(String.valueOf(productId));
        m1.setCategoryName("姓名：");
        m1.setCategory("输入项");
        MatchingInfoDO m2 = new MatchingInfoDO();
        m2.setProductId(String.valueOf(productId));
        m2.setCategoryName("年龄：");
        m2.setCategory("输入项");
        MatchingInfoDO m3 = new MatchingInfoDO();
        m3.setProductId(String.valueOf(productId));
        m3.setCategoryName("户籍");
        m3.setCategory("A 重庆本地 B 重庆外地");
        MatchingInfoDO m4 = new MatchingInfoDO();
        m4.setProductId(String.valueOf(productId));
        m4.setCategoryName("户口");
        m4.setCategory("A 城市户口 B 农村户口");
        MatchingInfoDO m5 = new MatchingInfoDO();
        m5.setProductId(String.valueOf(productId));
        m5.setCategoryName("资产信息名称");
        m5.setCategory("房产信息");
        MatchingInfoDO m6 = new MatchingInfoDO();
        m6.setProductId(String.valueOf(productId));
        m6.setCategoryName("按揭房所在区域");
        m6.setCategory("重庆全区县");
        MatchingInfoDO m7 = new MatchingInfoDO();
        m7.setProductId(String.valueOf(productId));
        m7.setCategoryName("土地类型");
        m7.setCategory("A 出让地 B 划拨地");
        MatchingInfoDO m8 = new MatchingInfoDO();
        m8.setProductId(String.valueOf(productId));
        m8.setCategoryName("房屋结构");
        m8.setCategory("A 钢筋混凝土结构 B 混合结构");
        MatchingInfoDO m9 = new MatchingInfoDO();
        m9.setProductId(String.valueOf(productId));
        m9.setCategoryName("房屋类型");
        m9.setCategory("A 住宅 B 洋房");
        list.add(m1);
        list.add(m2);
        list.add(m3);
        list.add(m4);
        list.add(m5);
        list.add(m6);
        list.add(m7);
        list.add(m8);
        list.add(m9);
        matchingInfoMapper.insertBatch(list);
    }

}
